package com.service.yzj.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.service.yzj.config.ApiResponse;
import com.service.yzj.dto.wx.SearchRequest;
import com.service.yzj.model.custom.CustomAddressInfo;
import com.service.yzj.model.order.OrderInfo;
import com.service.yzj.model.pages.wx.CustomPage;
import com.service.yzj.model.pages.wx.HomePage;
import com.service.yzj.model.pages.wx.SearchPage;
import com.service.yzj.model.product.ProductInfo;
import com.service.yzj.service.WxService;

/**
 * controller for wx mini program
 */
@RestController
@RequestMapping("/api/wx")
public class WXController {

  final WxService wxService;
  // final DAddressService addressService;
  final Logger logger;

  public WXController(
    @Autowired WxService wxService
  ) {
    this.wxService = wxService;
    this.logger = LoggerFactory.getLogger(WXController.class);
  }

  /**
   * 获取首页数据
   */
  @GetMapping(value = "/home")
  ApiResponse getHome() {
    logger.info("/api/wx/home get");
    HomePage homeData = wxService.getHomePage();

    return ApiResponse.ok(homeData);
  }

  /**
   * 搜索产品列表
   */
  @PostMapping(value = "/search")
  ApiResponse searchProducts(@RequestBody SearchRequest request) {
    logger.info("/api/wx/search post");
    logger.info("request: {}", request);

    int pageNumber;
    int pageSize;
    if (request.getPageNumber() != null) {
        pageNumber = request.getPageNumber().get();
    } else {
        pageNumber = 1;
    }

    if (request.getPageSize() != null) {
      pageSize = request.getPageSize().get();
    } else {
      pageSize = 10;
    }
    String cityCode = request.getCityCode();
    String keyword = request.getKeyword();

    SearchPage searchData = wxService.getProductsWithKeyword(pageNumber, pageSize, cityCode, keyword);
    return ApiResponse.ok(searchData);
  }

  /**
   * 获取产品数据
   */
  @GetMapping(value = "/product")
  ApiResponse getProductWithId(@RequestParam long id) {
    logger.info("/api/wx/product?id=" + id + " get");
    ProductInfo productInfo = wxService.getProductWithId(id);

    return ApiResponse.ok(productInfo);
  }

  /**
   * 获取用户地址列表
   * @return API response json
   */
  @GetMapping(value = "/my-address")
  ApiResponse getMyAddress() {
    logger.info("/api/wx/my-address get");
    List<CustomAddressInfo> allAddresses = wxService.getAllAddresses();

    return ApiResponse.ok(allAddresses);
  }

  /**
   * 获取我的订单
   * @return
   */
  @GetMapping(value = "/my-order")
  ApiResponse getMyOrder() {
    logger.info("/api/wx/my-order get");

    long userId = 1;
    OrderInfo orderInfo = wxService.getOrderWithUserId(userId);

    return ApiResponse.ok(orderInfo);
  }

  /**
   * 获取我的订单
   * @return
   */
  @GetMapping(value = "/my-info")
  ApiResponse getMyUserInfo() {
    logger.info("/api/wx/my-info get");

    long userId = 1;
    CustomPage customPage = wxService.getCustomerById(userId);

    return ApiResponse.ok(customPage);
  }
}